package com.xyf.sys.service.impl;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.xyf.sys.domain.OrderProduct;
import com.xyf.sys.domain.User;
import com.xyf.sys.enums.DeliveryEnum;
import com.xyf.sys.mapper.OrderProductMapper;
import com.xyf.sys.service.IOrderProductService;
import com.xyf.sys.vo.OrderUserResult;
import com.xyf.sys.vo.SaleCountResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class OrderProductServiceImpl implements IOrderProductService {

    @Autowired
    private OrderProductMapper orderProductMapper;

    @Override
    public OrderProduct get(String id) {
        return orderProductMapper.selectByPrimaryKey(id);
    }

    @Override
    public void save(OrderProduct orderProduct) {
        orderProductMapper.insert(orderProduct);
    }

    @Override
    public void saveAll(List<OrderProduct> products) {
        for (OrderProduct product : products) {
            orderProductMapper.insert(product);
        }
    }

    @Override
    public List<OrderProduct> getOrderProductList(String orderId) {
        List<OrderProduct> productList = orderProductMapper.selectByOrderId(orderId);
        if (!CollectionUtils.isEmpty(productList)){
            for (OrderProduct orderProduct : productList) {
                if (StringUtils.isNotBlank(orderProduct.getDeliveryId()) && DeliveryEnum.YUNDA.toString().equals(orderProduct.getDeliveryId()) && StringUtils.isNotBlank(orderProduct.getBigWord()) && StringUtils.isNotBlank(orderProduct.getRemark()) ){
                    String bigWord = orderProduct.getBigWord();
                    int i = bigWord.split(" ").length - 1;
                    if (i == 1){
                        String remark = orderProduct.getRemark();
                        JSONObject jsonObject = JSONObject.parseObject(remark);
                        String position = jsonObject.getString("position");
                        if (StringUtils.isNotBlank(position)){
                            String waybillData = orderProduct.getWaybillData();
                            JSONArray array = JSONArray.parseArray(waybillData);
                            JSONArray data = new JSONArray(2);
                            if (array != null && array.size() > 0){
                                for (int j = 0; j < array.size(); j++) {
                                    JSONObject jsonObject1 = array.getJSONObject(j);
                                    if (jsonObject1.containsKey("bigWord")){
                                        String bigWord1 = jsonObject1.getString("bigWord");
                                        jsonObject1.put("bigWord", position + " " + bigWord1);
                                    }
                                    data.add(jsonObject1);
                                }
                                orderProduct.setWaybillData(data.toJSONString());
                            }
                        }
                    }
                }
            }
        }
        return productList;
    }

    @Override
    public int update(OrderProduct orderProduct) {
        orderProduct.setLastUpdateTime(new Date());
        return orderProductMapper.updateByPrimaryKey(orderProduct);
    }

    @Override
    public OrderProduct query(String code) {
        return orderProductMapper.selectByCode(code);
    }

    @Override
    public Integer count(Long goodsId, String date) {
        return orderProductMapper.count(goodsId, date);
    }

    @Override
    public List<User> findGoodsUser(Long goodsId) {
        return orderProductMapper.selectLastOrderUser(goodsId);
    }

    @Override
    public List<OrderUserResult> findGoodsUsers(List<Long> goodsIds) {
        if (goodsIds.isEmpty()) {
            return Collections.emptyList();
        }
        return orderProductMapper.findGoodsUsers(goodsIds);
    }

    @Override
    public OrderProduct getCartItemProduct(String orderId, Long cartItemId) {
        return orderProductMapper.selectByOrderIdAndCartItemId(orderId, cartItemId);
    }

    @Override
    public OrderProduct find(String orderNumber) {
        return orderProductMapper.selectByWaybillId(orderNumber);
    }

    @Override
    public List<SaleCountResult> countSaleAndTotalStock(List<Long> goodsIds, String date) {
        return orderProductMapper.countSaleAndTotalStock(goodsIds, date);
    }
}
